﻿using FtpLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace YouCacheIt_FTP
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private object dummyTreeNode = null;
        private object ftpdummyTreeNode = null;

        public MainWindow()
        {
            InitializeComponent();
            loadDirectory();
        }

        private void loadDirectory()
        {
            foreach (string s in Directory.GetLogicalDrives())
            {
                TreeViewItem item = new TreeViewItem();
                item.Header = s;
                item.Tag = s;
                item.FontWeight = FontWeights.Normal;
                item.Items.Add(dummyTreeNode);
                item.Expanded += new RoutedEventHandler(folder_Expanded);
                LocalServer.Items.Add(item);
            }
        }


        void folder_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;
            if (item.Items.Count == 1 && item.Items[0] == dummyTreeNode)
            {
                item.Items.Clear();
                try
                {
                    foreach (string s in Directory.GetDirectories(item.Tag.ToString()))
                    {
                        TreeViewItem subitem = new TreeViewItem();
                        subitem.Header = s.Substring(s.LastIndexOf("\\") + 1);
                        subitem.Tag = s;
                        subitem.FontWeight = FontWeights.Normal;
                        subitem.Items.Add(dummyTreeNode);
                        subitem.Expanded += new RoutedEventHandler(folder_Expanded);
                        item.Items.Add(subitem);
                    }
                }
                catch (Exception) { }
            }
        }

        private void LocalServer_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            findParentFolder((TreeViewItem)e.NewValue);
        }

        private void findParentFolder(TreeViewItem temp)
        {
            if (temp != null)
            {
                TreeView tree = LocalServer;
                string path = "";
                string temp1 = "";
                string temp2 = "";
                FileInfo info;
                while (true)
                {
                    temp1 = temp.Header.ToString();
                    if (temp1.Contains(@"\"))
                        temp2 = "";
                    path = temp1 + temp2 + path;
                    if (temp.Parent.GetType().ToString() == "System.Windows.Controls.TreeView")
                        break;
                    temp = ((TreeViewItem)temp.Parent);
                    temp2 = @"\";
                }
                LocalLocation.Text = path;
                try
                {
                    string[] files = Directory.GetFiles(path);
                    List<FileDetails> allfiles = new List<FileDetails>();
                    foreach(string file in files)
                    {
                        info = new FileInfo(file);
                        allfiles.Add(new FileDetails() { FileName = info.Name, FileSize = info.Length.ToString(), FileType = info.Extension, LastModified = info.LastAccessTime.ToString() });
                    }
                   DirectoryFiles.ItemsSource = allfiles;
                }
                catch (Exception e) 
                {
                    DirectoryFiles.ItemsSource = null;
                }
            }   
        }

        private void connectClick(object sender, RoutedEventArgs e)
        {
            ClosableTab tabItem = new ClosableTab();
            tabItem.Title = hostText.Text;
            //FTP NewConnection = new FTP(hostText.Text, hostUsername.Text, hostPassword.Password);
            FtpConnection ftp = new FtpConnection(hostText.Text, hostUsername.Text, hostPassword.Password);
            ftp.Open();
            ftp.Login();
            //FtpDirectoryInfo[] ftpdirs = ftp.GetDirectories();
            
           // MessageBox.Show(ftp.GetFileSize("index.html").ToString());
            tabItem.Content = loadFtpDirectory(ftp.GetDirectories());
            RemoteServer.Items.Add(tabItem);
           // RemoteServer.Items.Add(loadFtpDirectory(ftp.GetDirectories()));
            tabItem.Focus();
        }

        private TreeView loadFtpDirectory(FtpDirectoryInfo[] ftpdirs)
        {
            TreeView newview = new TreeView();
            foreach (FtpDirectoryInfo s in ftpdirs)
            {
                TreeViewItem item = new TreeViewItem();
                item.Header = s.Name;
                item.Tag = s.Name;
                item.FontWeight = FontWeights.Normal;
                item.Items.Add(ftpdummyTreeNode);
                item.Expanded += new RoutedEventHandler(ftpfolder_Expanded);
                newview.Items.Add(item);
            }
            return newview;
        }

        void ftpfolder_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;
            if (item.Items.Count == 1 && item.Items[0] == ftpdummyTreeNode)
            {
                item.Items.Clear();
                try
                {
                    foreach (FtpDirectoryInfo s in FtpDirectoryInfo Directory.GetDirectories(item.Tag.ToString()))
                    {
                        TreeViewItem subitem = new TreeViewItem();
                        subitem.Header = s.Substring(s.LastIndexOf("\\") + 1);
                        subitem.Tag = s;
                        subitem.FontWeight = FontWeights.Normal;
                        subitem.Items.Add(dummyTreeNode);
                        subitem.Expanded += new RoutedEventHandler(ftpfolder_Expanded);
                        item.Items.Add(subitem);
                    }
                }
                catch (Exception) { }
            }
        }
    }
}

